<?php

namespace App\Imports;

use Illuminate\Support\Facades\Hash;
// use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;
use App\Models\Admin\ReviewOrder;
use DB, Auth;

class ReviewOrderImport implements ToCollection
{
    
    public function collection(Collection $row)
    {   
        // 读取店铺ID
        $user = Auth::guard('admin')->user();
        $storeId = $user->store_id;

        $arrOrder = [];
        $id = 0;
        foreach ($row as $key => $value) {
            if($key==0){
                continue;
            }
            if($storeId != 0 && $storeId == trim($value[0])){
                if (!empty(trim($value[1]))) {
                    array_push($arrOrder,$value[1]);
                }
                $id = $storeId;
            }else if($storeId == 0){
                if (!empty(trim($value[1]))) {
                    array_push($arrOrder,$value[1]);
                }
                $id = $value[0];
            }
            
        }
        $arrOrder=implode(",",array_filter($arrOrder));
        $review =  new ReviewOrder;

        if($id != 0){
            $info = $review->where('store_id',$id)->first();
            if(!empty($info)){
                $info->content = $arrOrder;
                $info->save();
            }else{
                $review->insert(['store_id'=>$id,'content'=>$arrOrder]);
            }
        }
        
    }
}